<template>
	<!-- 隐患整改下发流程页面 -->
	<view class="workApply-container" :style="{ height: `${windowHeight}px` }">
		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" border>

			<!-- 部门下发 -->
			<template v-if="rowData.personDirectlyResponsible == null &&rowData.teamHeadId == null">
				<uni-forms-item label="是否直接负责人：" required name="whetherDutyOfficer" class="view-select" >
					<uni-data-select v-model="valiFormData.whetherDutyOfficer"
						:localdata="whetherDutyOption"></uni-data-select>
				</uni-forms-item>
				<template v-if="valiFormData.whetherDutyOfficer == 2" >
					<uni-forms-item label="整改班组：" required name="hiddenDangerDept">
					
						<custom-tree-select :listData="treeData" dataLabel="label" dataValue="id" :search="true"
							:mutiple="false" v-model="valiFormData.hiddenDangerDept" :showChildren="true"
							@input="depChange" />
						</custom-tree-select>
					
					</uni-forms-item>
					<!-- 上面选择检查计划回写上来的 -->
					<uni-forms-item label="班组级整改人：" required name="teamHeadId">
					
						<custom-tree-select :listData="deptUserList" dataLabel="nickName" dataValue="userId" :search="true"
							:mutiple="false" v-model="valiFormData.teamHeadId" />
					
						</custom-tree-select>
					</uni-forms-item>
				</template>
				
			</template>
			<!--  班组下发 v-if="valiFormData.whetherDutyOfficer == 2 && rowData.personDirectlyResponsible == null && rowData.teamHeadId != null"-->
			<template v-if="rowData.personDirectlyResponsible == null &&rowData.teamHeadId !== null">
				<uni-forms-item label="是否直接负责人：" required name="whetherDutyOfficer">
					<uni-data-select v-model="valiFormData.whetherDutyOfficer"
						:localdata="whetherDutyOption"></uni-data-select>

				</uni-forms-item>
				<uni-forms-item label="整改人：" required name="personDirectlyResponsible"   v-if="valiFormData.whetherDutyOfficer == 2">

					<custom-tree-select :listData="allUsers" dataLabel="nickName" dataValue="userId" :search="true"
						:mutiple="false" v-model="valiFormData.personDirectlyResponsible" :showChildren="true" />

					</custom-tree-select>

				</uni-forms-item>

			</template>
			<!-- 整改计划填写 -->
			<template v-if="rowData.personDirectlyResponsible !== null || valiFormData.whetherDutyOfficer == 1">
				<uni-card title="整改计划填写" :isFull="true" sub-title="" extra="">

					<uni-forms-item label="整改订单编号：" required name="orderNumber">
						<uni-easyinput v-model="valiFormData.orderNumber" placeholder="请输入" :inputBorder="false" />
					</uni-forms-item>

					<uni-forms-item label="计划完成时间：" required name="planRectificationTime">

						<uni-datetime-picker type="date" :clear-icon="false"
							v-model="valiFormData.planRectificationTime" />


					</uni-forms-item>
					<uni-forms-item label="整改措施：" required name="rectificationMeasures">

						<uni-easyinput v-model="valiFormData.rectificationMeasures" type="textarea" placeholder="请输入"
							:inputBorder="false" />


					</uni-forms-item>
					<uni-forms-item label="计划整改金额(万元)：" required name="planRectificationMoney">
						<uni-easyinput v-model="valiFormData.planRectificationMoney" type="number" placeholder="请输入"
							:inputBorder="false" />

					</uni-forms-item>
				</uni-card>
			</template>
		</uni-forms>

		<button type="primary" @click="submit('valiForm')" class="submit-btn">
			确认
		</button>


	</view>
</template>

<script>
	import widowHeightMixins from "@/mixins/widowHeightMixins.js";

	import {
		getNowTime
	} from "@/utils/nowTime.js"
	import {
		addAllShoot,
	} from "@/api/hiddenTrouble/allShoot.js";
	import {
		distributeRect,
	} from "@/api/hiddenTrouble/rectification.js";
	import {
		listUser
	} from "@/api/system/user.js"
	import {
		deptTreeSelect
	} from "@/api/system/user";
	import {
		mapGetters
	} from "vuex"
	export default {
		mixins: [widowHeightMixins],
		components: {

		},
		onLoad(e) {
			// console.log(e, "e");
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpenedPage', (data) => {
				this.queryForm.pageStatus = data.pageStatus
				this.valiFormData.id = data.checkId
				this.rowData = data.rowData
				console.log("接受", data)



			});
			// 动态设置标题
			uni.setNavigationBarTitle({
				title: `隐患整改--下发`,
			});
		},
		data() {
			return {
				rowData: {},
				whetherDutyOption: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 2
					},
				],
				//部门下用户列表
				deptUserList: [],
				//人员的格式化
				configParams: {
					valueKey: "userId",
					labelKey: "nickName"
				},
				//隐患登记
				hiddenForm: {},
				//详情
				detailObj: {},
				hiddenLevelOption: [{
						text: '一般隐患',
						value: 1
					},
					{
						text: '重大隐患',
						value: 2
					},

				],
				solveOption: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 2
					},
				],
				//隐患登记表
				hiddentroubleInspectionRegisters: [],
				//当前选中
				current: 0,

				//选中ID
				checkId: null,
				//操作状态
				operateStatus: null,
				//上传参数对象
				queryForm: {},
				//手工选择弹窗
				manualVisible: false,
				//被检查单位的数据
				treeData: [],
				// 校验表单数据
				valiFormData: {},
				taskLevelOption: [],


				// 数据总量
				total: 0,
				loading: false,
				//检查执行人
				executorList: [],


				// 校验规则
				rules: {
					dept: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					name: {
						rules: [{
							required: true,
							errorMessage: "姓名不能为空",
						}, ],
					},
					age: {
						rules: [{
								required: true,
								errorMessage: "年龄不能为空",
							},
							{
								format: "number",
								errorMessage: "年龄只能输入数字",
							},
						],
					},
				},
			};
		},
		methods: {
			/** 部门的change事件 */
			async depChange(val) {
				let query = {
					deptId: val[0],

				};
				let res = await listUser(query);
				this.deptUserList = res.rows;

			},


			//处理详情接口数据回写上面
			handleopen(data) {

			},
			/** 被检查部门部门下拉树结构 */
			async getDeptTree() {
				const response = await deptTreeSelect();
				this.treeData = response.data;
			},


			init() {

				let res = uni.getStorageSync('userInfo')

				this.$set(this.valiFormData, "companyHeadId", res.userId)

			},
			submit(ref) {
				this.$refs[ref]
					.validate()
					.then(async (res) => {

						this.valiFormData.hiddenDangerDept = this.valiFormData.hiddenDangerDept?.[0]
						this.valiFormData.teamHeadId = this.valiFormData.teamHeadId?.[0]
						this.valiFormData.personDirectlyResponsible = this.valiFormData.personDirectlyResponsible
							?.[0]
						console.log("提交参数", this.valiFormData)
						let response = await distributeRect(this.valiFormData);
						uni.showToast({
							title: `提交成功`,
							duration: 2000
						});
						uni.navigateTo({
							url: "/pages/trouble/index",
							success: (res) => {
								res.eventChannel.emit('acceptDataFromOpenerPage', {
									page: 'manage',
									title: '计划反馈'
								})
							}
						})


					})
					.catch((err) => {
						console.log("err", err);
					});
			},
		},
		mounted() {
			this.getDeptTree()
			this.init()
		},
		computed: {
			...mapGetters(["allUsers", "deptId"]),
		}
	};
</script>

<style lang="scss" scoped>
	.workApply-container {
		padding: 20rpx;
		background-color: #fff;
		overflow-y: auto;

		::v-deep {
			.uni-easyinput {
				border: 1px solid #e5e5e5;
			}
			.view-select{
				.uni-select__selector{
					top:66rpx;
				}
			}

			.uni-forms-item--border {
				border-top: 0 solid #000;
				border-bottom: 2rpx solid #e8eaec;
			}

			.uni-forms-item__label {
				width: 40% !important;
			}

			.submit-btn {
				margin-top: 30rpx;
				background: #137ee9;
				border-radius: 10rpx;
			}
		}
	}
</style>